package com.detech.sbom.base.config.swagger;

import com.detech.sbom.base.annotation.ProductProfile;
import com.detech.sbom.base.annotation.ProductSwagger;
import com.detech.sbom.base.enumtype.CodeEnum;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@EnableOpenApi  //swagger 3
@EnableKnife4j  //Knife4j
@Configuration
@ProductProfile //此配置仅在product环境下生效
public class SwaggerConfigProduct {

    @Bean
    public Docket createRestApiForProduct() {
        //添加全局响应状态码
        List<Response> responseList = new ArrayList<>();
        Arrays.stream(CodeEnum.values()).forEach(codeEnum -> {
            responseList.add(
                    new ResponseBuilder().code(Integer.toString(codeEnum.getCode())).description(codeEnum.getMsg()).build()
            );
        });
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false)
                .globalResponses(HttpMethod.GET, responseList)
                .globalResponses(HttpMethod.POST, responseList)
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ProductSwagger.class))
                .paths(PathSelectors.any()).build().groupName("生产环境下可见Api").pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot样例项目")
                .termsOfServiceUrl("")
                .description("")
                .version("1.0")
                .build();
    }

}
